package com.zhan.signalling.data.listener;

import java.util.List;
import java.util.Map;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.listener.DataListener;
import com.zhan.signalling.check.AccoutPermissionCheck;
import com.zhan.signalling.commons.Constants;
import com.zhan.signalling.commons.SocketEvent;
import com.zhan.signalling.entity.WhiteBoardRecord;
import com.zhan.signalling.message.Message;
import com.zhan.signalling.message.Result;
import com.zhan.signalling.store.StoreMessageFactory;
import com.zhan.signalling.store.StoreMessageStore;
import com.zhan.signalling.util.RedisUtil;

/**
 * 获取历史记录
 * @author knight
 *
 */
public class HistoryWhiteBoardListener implements DataListener<Message> {
	
	private StoreMessageStore dataHandler;
	
	public HistoryWhiteBoardListener() {
		dataHandler = StoreMessageFactory.getHandler();
		dataHandler.initStore(WhiteBoardRecord.class);
	}
	
	public void onData(SocketIOClient client, Message data, AckRequest ackSender) throws Exception {
		
		// 校验权限
		AccoutPermissionCheck.check(client, SocketEvent.HISTORY_ＷHITEBOARD_EVENT);
		String groupId = client.get(Constants.GROUP_KEY);
		String platform = client.getNamespace().getName();
		Long seqId = RedisUtil.lastSeqId(platform + ":" + groupId + ":" + SocketEvent.WHITEBOARD_EVENT);
		List<Map<String,String>> result = dataHandler.getHistoryMessage(platform, groupId, seqId);
		client.sendEvent(SocketEvent.HISTORY_ＷHITEBOARD_EVENT, new Result(result, Constants.SUCCESS));
	}

}
